﻿/*
 * Twipler - Twitter Web Client with features.
 *  
 *  Copyright (C) 2009, 2010. Ian Quigley
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details. 
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Twipler.App;
using Twipler.Data.Access;
using Twipler.Data.Entity.Theme;
using Twipler.Data.Entity.Twipler;
using Twipler.WebApp;

using NHibernate.Criterion;

namespace Settings.Theme
{
    [QueryStringHash]
    [RequireUser(VisitorRole.Registered)]
    public partial class Select : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ProcessRequest();
            
            if (IsPostBack)
                return;

            List<Design> designs = new List<Design>();

            if (!Current.Visitor.HasRole(VisitorRole.ThemeCreator))
            {
                pnlCreateNew.Visible = false;
            }
            else
            {
                designs.Add(new Design() { Name = "Blank", Id = 0 });
                designs.AddRange(Factory.Design.GetByCriteria(
                    Expression.And(
                        Expression.Eq("State", (int)DesignState.Published),
                        Expression.Eq("AllowCopy", true))));

                drpList.DataSource = designs;
                drpList.DataTextField = "Name";
                drpList.DataValueField = "Id";
                drpList.DataBind();
            }

            StringBuilder html = new StringBuilder();

            designs = Factory.Design.GetByCriteria(Expression.Eq("State", (int)DesignState.Published));
            if (designs.Count() > 0)
            {
                html.Append("<h3>Published</h3>");
                ListDesigns(html, designs);
            }

            designs = Factory.Design.GetByCriteria(Expression.And(
                                                       Expression.Eq("CreatedBy", Current.Visitor),
                                                       Expression.Eq("State", (int)DesignState.Private)));

            if (designs.Count() > 0)
            {
                html.Append("<h3>Your private designs</h3>");
                ListDesigns(html, designs);
            }

            litStuff.Text = html.ToString();

            Current.PageProducer = null;
        }

        private void ListDesigns(StringBuilder html, List<Design> designs)
        {
            foreach (Design design in designs)
            {
                html.AppendFormat("<div style=\"padding: 5px;\"><A href=\"{0}\">{1}</a></div>",
                                   QueryStringHash.Protect(string.Format("select.aspx?setdesign={0}", design.Id)), design.Name);
            }
        }

        private void ProcessRequest()
        {
            if (Request.QueryString["setdesign"] != null)
            {
                int designId;
                if (int.TryParse( Request.QueryString[ "setdesign" ] , out designId))
                {
                    Design selected = Factory.Design.GetById(designId, false);
                    if (selected != null)
                    {
                        Current.Visitor.Design = selected;
                        Factory.Visitor.SaveOrUpdate(Current.Visitor);

                        if (selected.AllowStyle)
                            Response.Redirect(QueryStringHash.Protect("selectstyle.aspx?designid=" + designId));
                        else
                            Response.Redirect("../../default.aspx");
                    }
                }
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            Design newDesign;
            if (drpList.SelectedValue != "0")
            {
                Design copy = Factory.Design.GetById(int.Parse(drpList.SelectedValue), false);
                newDesign = (Design)copy.Clone();
            }
            else
            {
                newDesign = new Design();
            }


            Factory.Design.SaveOrUpdate(newDesign);
            Response.Redirect(QueryStringHash.Protect("designlist.aspx?edit=" + newDesign.Id));
            
        }
    }
}